require(["core", "bus_permission", "jQuery", "coAjax", "hash_routie", "queryString", "href", "eventManager", "versionManage", "appConfigAdmin"], function(jSouper, bus_permission, $, coAjax, hash_routie, QueryString, href, eventManager, versionManage) {

	var _page_num = 10;
	var _showing_num = _page_num;
	hash_routie.on("busmanager", function() {
		console.log("appConfig.admin.get_bus_list:", appConfig.admin.get_bus_list);
		coAjax.get(appConfig.admin.get_bus_list, function(result) {
			var bus_list = result.result;
			jSouper.forEach(bus_list, function(bus) {
				bus.status = ~~bus.status || 0
			});
			App.set("$Cache.admin.busmanager.bus_list", bus_list);
			App.set("$Cache.admin.busmanager.showing_bus_list", bus_list.slice(0, _showing_num));
		});
		coAjax.get(appConfig.dotnar_template_list, function(result) {
			var template_list = result.result;
			jSouper.forEach(template_list, function(template_item) {
				try {
					template_item.package = JSON.parse(template_item.package);
				} catch (e) {}
			});
			App.set("$Cache.admin.busmanager.dotnar_template_list", template_list);
		});
	});
	//商家移除
	App.set("$Event.admin.busmanager.remove_bus", function(e, vm) {
		myConfirm("确定要删除“" + vm.get("info.name") + "”这个商家？", function() {
			coAjax.delete(appConfig.admin.remove_bus, {
				bus_id: vm.get("_id")
			}, function(result) {
				alert("success", "删除成功")
				App.set("$Cache.admin.busmanager.bus_list", result.result);
				App.set("$Cache.admin.busmanager.showing_bus_list", result.result.slice(0, _showing_num));
			});
		}, function() {
			alert("删除已取消");
		});
	});
	//审核状态的处理事件
	var BUS_STATE = appConfig.admin.BUS_STATE;
	for (var state in BUS_STATE) {
		if (BUS_STATE.hasOwnProperty(state)) {;
			(function(state) {
				App.set("$Event.admin.bus_state.to_" + state, function(e, vm) {
					coAjax.put(appConfig.admin.update_bus_state, {
						bus_id: vm.get("_id"),
						state: appConfig.admin.BUS_STATE[state]
					}, function(result) {
						vm.set(result.result);
					});
				});
			}(state));
		}
	}
	//展开、隐藏设置
	App.set("$Event.admin.busmanager.open_setting", function(e, vm) {
		//因为是绑定到customTag上，所以需要$Caller前缀
		vm.set("$Private.$Cache.show_setting", true);
	});
	App.set("$Event.admin.busmanager.close_setting", function(e, vm) {
		vm.set("$Private.$Cache.show_setting", false);
	});
	//权限项设置：
	jSouper.forEach([
		"can_set_goods_call_back_buff",
		"can_sale_virtual_goods",
		"can_only_online_pay",
		"can_invite_bus",
		"can_set_theme",
		"can_goods_be_bina",
		"can_bina_to_third",
		"bina_no_need_buy",
		"can_multi_bina",
		"data_management_category",
		"data_pc_template_name",
		"data_mobile_template_name",
		"can_municipal_agency"
	], function(permission_key) {
		App.set("$Event.admin.bus_" + permission_key + ".toggle", function(e, vm) {
			coAjax.put(appConfig.admin.bus_permission + permission_key, {
				value: vm.get("permission." + permission_key),
				bus_id: vm.get("_id")
			}, function(result) {
				alert("success", "修改成功");
				vm.set(result.result);
			});
		});
	});
	//商家到期时间设置
	App.set("$Event.admin.update_bus_expiration_time", function(e, vm) {
		coAjax.put(appConfig.admin.bus_expiration_time, {
			bus_id: vm.get("_id"),
			expiration_time: vm.get("expiration_time")
		}, function(result) {
			alert("success", "修改成功");
			vm.set(result.result);
		});
	});
	//商家手机版风格包设置
	App.set("$Event.admin.set_bus_mobile_theme_name", function(e, vm) {
		coAjax.put(appConfig.admin.bus_mobile_theme_name, {
			mobile_theme_name: vm.get("permission.data_mobile_theme_name"),
			bus_id: vm.get("_id")
		}, function(result) {
			alert("success", "修改成功");
			vm.set(result.result);
		});
	});
	// //商家经营类型设置
	// App.set("$Event.admin.set_bus_management_category", function(e, vm) {
	// 	coAjax.put(appConfig.admin.bus_management_category, {
	// 		management_category: vm.get("permission.data_management_category"),
	// 		bus_id: vm.get("_id")
	// 	}, function(result) {
	// 		alert("success", "修改成功");
	// 		vm.set(result.result);
	// 	});
	// });
	//商家最大商品数量限制
	var _data_max_goods_num_ti
	App.set("$Event.admin.set_bus_max_goods_num", function(e, vm) {
		clearTimeout(_data_max_goods_num_ti);
		_data_max_goods_num_ti = setTimeout(function() {
			coAjax.put(appConfig.admin.bus_permission + "data_max_goods_num", {
				value: vm.get("permission.data_max_goods_num"),
				bus_id: vm.get("_id")
			}, function(result) {
				alert("success", "商品数量限制修改成功");
				vm.set(result.result);
			});
		}, 200)
	});

	//显示更多商家
	App.set("$Event.admin.busmanager.show_more", function() {
		var bus_list = App.get("$Cache.admin.busmanager.bus_list");
		var showing_bus_list_length = App.get("$Cache.admin.busmanager.showing_bus_list.length");
		_showing_num = showing_bus_list_length + _page_num;
		var showing_bus_list = bus_list.slice(0, _showing_num);
		App.set("$Cache.admin.busmanager.showing_bus_list", showing_bus_list);
	});
	//模糊搜索
	// var back_bus_list;
	App.set("$Event.admin.busmanager.search", function() {
		var search_text = jSouper.trim(App.get("$Cache.admin.busmanager.search_text")).toLowerCase();
		if (search_text) {
			var bus_list = App.get("$Cache.admin.busmanager.bus_list");
			var filter_bus_list = jSouper.filter(bus_list, function(bus) {
				for (var i in bus.info) {
					var value = bus.info[i];
					if (typeof value === "string" && value.toLowerCase().indexOf(search_text) !== -1) {
						return true;
					}
				}
			});
			filter_bus_list.length > 10 && (filter_bus_list.length = 10);
			App.set("$Cache.admin.busmanager.searching_bus_list", filter_bus_list);
		}
		App.set("$Cache.admin.busmanager.is_searching", !!search_text);
	});
	//排序
	App.set("$Event.admin.busmanager.bus_sort", function() {
		var bus_sort_way = !App.get("$Cache.admin.busmanager.bus_sort_way")
		App.set("$Cache.admin.busmanager.bus_sort_way", bus_sort_way);
		var bus_list = App.get("$Cache.admin.busmanager.bus_list");
		bus_list = bus_list.reverse();
		App.set("$Cache.admin.busmanager.showing_bus_list", bus_list.slice(0, _showing_num));

		var searching_bus_list = App.get("$Cache.admin.busmanager.searching_bus_list");
		searching_bus_list && App.set("$Cache.admin.busmanager.searching_bus_list", searching_bus_list.reverse());
	});
	//导航设置
	var _ti;
	App.set("$Event.admin.busmanager.update_bus_perms", function(e, vm) {
		clearTimeout(_ti);
		_ti = setTimeout(function() {
			coAjax.put(appConfig.admin.bus_nav_perms, {
				perms_json: JSON.stringify(bus_permission.sort(vm.get("$Caller.$Caller.perms"))), //上传可操作的数据
				bus_id: vm.get("$Caller.$Caller._id")
			}, function(result) {
				var bus_info = result.result;
				vm.set("$Caller.$Caller.perms", bus_info.perms); //实际数据
				alert("success", "修改导航设置成功")
			});
		}, 200);
	});
	App.set("$Event.admin.reset_bus_perms", function(e, vm) {
		coAjax.put(appConfig.admin.bus_nav_perms, {
			perms_json: this.checked ? 'null' : JSON.stringify(bus_permission.default_pers),
			bus_id: vm.get("$Caller.$Caller._id")
		}, function(result) {
			var bus_info = result.result;
			vm.set("perms", bus_info.perms);
			alert("success", "修改导航设置成功");
		});
	});
});